﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Window2.xaml 的交互逻辑
    /// </summary>
    public partial class Window2 : Window
    {
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() => Task1());
            Task.Run(() => Task2());
            Task.Run(() => Task3());
            Task.Run(() => Task4());
            Task.Run(() => Task5());
            Task.Run(() => Task6());
            Task.Run(() => Task7());
            Task.Run(() => Task8());
            Task.Run(() => Task9());
            Task.Run(() => Task10());
            Task.Run(() => Task11());
            Task.Run(() => Task12());
        }

        void Task1()
        {            
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10001);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt1.Text = result;
                    if (result.Contains("ST"))
                    {
                        border1.Background = Brushes.Green;
                    }
                    else
                    {
                        border1.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task2()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10002);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt2.Text = result;
                    if (result.Contains("ST"))
                    {
                        border2.Background = Brushes.Green;
                    }
                    else
                    {
                        border2.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task3()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10003);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt3.Text = result;
                    if (result.Contains("ST"))
                    {
                        border3.Background = Brushes.Green;
                    }
                    else
                    {
                        border3.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task4()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10004);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt4.Text = result;
                    if (result.Contains("ST"))
                    {
                        border4.Background = Brushes.Green;
                    }
                    else
                    {
                        border4.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task5()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10005);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt5.Text = result;
                    if (result.Contains("ST"))
                    {
                        border5.Background = Brushes.Green;
                    }
                    else
                    {
                        border5.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task6()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10006);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt6.Text = result;
                    if (result.Contains("ST"))
                    {
                        border6.Background = Brushes.Green;
                    }
                    else
                    {
                        border6.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task7()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10007);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt7.Text = result;
                    if (result.Contains("ST"))
                    {
                        border7.Background = Brushes.Green;
                    }
                    else
                    {
                        border7.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task8()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10008);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt8.Text = result;
                    if (result.Contains("ST"))
                    {
                        border8.Background = Brushes.Green;
                    }
                    else
                    {
                        border8.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task9()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10009);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt9.Text = result;
                    if (result.Contains("ST"))
                    {
                        border9.Background = Brushes.Green;
                    }
                    else
                    {
                        border9.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task10()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10010);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt10.Text = result;
                    if (result.Contains("ST"))
                    {
                        border10.Background = Brushes.Green;
                    }
                    else
                    {
                        border10.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task11()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10011);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt11.Text = result;
                    if (result.Contains("ST"))
                    {
                        border11.Background = Brushes.Green;
                    }
                    else
                    {
                        border11.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
        void Task12()
        {
            var socket1 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket1.Connect("192.168.10.101", 10012);
            while (true)
            {
                var buff = new byte[4096];
                var len = socket1.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);
                this.Dispatcher.BeginInvoke(new Action(() => {
                    txt12.Text = result;
                    if (result.Contains("ST"))
                    {
                        border12.Background = Brushes.Green;
                    }
                    else
                    {
                        border12.Background = Brushes.Red;
                    }
                }));
                Thread.Sleep(100);
            }
        }
    }
}

public static class TcpClientEx
{
    public static bool IsOnline(this TcpClient c)
    {
        return !((c.Client.Poll(1000, SelectMode.SelectRead) && (c.Client.Available == 0)) || !c.Client.Connected);
    }
}
